package admin

import (
	"context"

	"lianzi_gos/internal/mods/admin/api"
	"lianzi_gos/internal/mods/mpweixin/schema"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type ADMIN struct {
	DB            *gorm.DB
	BannerAPI     *api.Banner
	StaffAPI      *api.Staff
	MemberAPI     *api.Member
	BrandAPI      *api.Brand
	BusinessAPI   *api.Business
	SyscfgAPI     *api.Syscfg
	GoodsAPI      *api.Goods
	RecordAPI     *api.Record
	ProcuresAPI   *api.Procure
	CategoryAPI   *api.Category
	ConnectionAPI *api.Connection
}

func (a *ADMIN) AutoMigrate(ctx context.Context) error {
	return a.DB.AutoMigrate(new(schema.Banner))
}

func (a *ADMIN) Init(ctx context.Context) error {
	if err := a.AutoMigrate(ctx); err != nil {
		return err
	}
	return nil
}

func (a *ADMIN) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup) error {
	banner := v1.Group("admin_banners")
	{
		banner.GET("", a.BannerAPI.Query)
		banner.GET(":id", a.BannerAPI.Get)
		banner.POST("", a.BannerAPI.Create)
		banner.PUT(":id", a.BannerAPI.Update)
		banner.DELETE(":id", a.BannerAPI.Delete)
	}
	staff := v1.Group("admin_staff")
	{
		staff.GET("", a.StaffAPI.Query)
		staff.GET(":id", a.StaffAPI.Get)
		staff.POST("", a.StaffAPI.Create)
		staff.PUT(":id", a.StaffAPI.Update)
		staff.DELETE(":id", a.StaffAPI.Delete)
		staff.GET("qiye", a.StaffAPI.Qiye)
	}
	member := v1.Group("admin_member")
	{
		member.GET("", a.MemberAPI.Query)
		member.GET(":id", a.MemberAPI.Get)
		member.POST("", a.MemberAPI.Create)
		member.PUT(":id", a.MemberAPI.Update)
		member.DELETE(":id", a.MemberAPI.Delete)
	}
	// 业态
	business := v1.Group("admin_business")
	{
		business.GET("", a.BusinessAPI.Query)
		business.GET(":id", a.BusinessAPI.Get)
		business.POST("", a.BusinessAPI.Create)
		business.PUT(":id", a.BusinessAPI.Update)
		business.DELETE(":id", a.BusinessAPI.Delete)
	}
	// 品牌
	brands := v1.Group("admin_brands")
	{
		brands.GET("", a.BrandAPI.Query)
		brands.GET(":id", a.BrandAPI.Get)
		brands.POST("", a.BrandAPI.Create)
		brands.PUT(":id", a.BrandAPI.Update)
		brands.DELETE(":id", a.BannerAPI.Delete)
	}
	// 分类
	categories := v1.Group("admin_categories")
	{
		categories.GET("", a.CategoryAPI.Query)
		categories.GET(":id", a.CategoryAPI.Get)
		categories.POST("", a.CategoryAPI.Create)
		categories.PUT(":id", a.CategoryAPI.Update)
		categories.DELETE(":id", a.CategoryAPI.Delete)
	}
	// 公司简介
	sysconfig := v1.Group("sysconfig")
	{
		sysconfig.GET(":id", a.SyscfgAPI.Get)
		sysconfig.PUT(":id", a.SyscfgAPI.Update)
	}
	// 商品
	product := v1.Group("product")
	{
		product.GET(":id", a.GoodsAPI.Get)
		product.GET("", a.GoodsAPI.Query)
		product.DELETE(":id", a.GoodsAPI.Delete)
	}
	//我要买
	procure := v1.Group("admin_procure")
	{
		procure.GET("", a.ProcuresAPI.QueryAdmin)
	}
	// 商品 跟踪记录
	records := v1.Group("admin_records")
	{
		//product.GET(":id", a.RecordAPI.Get)
		records.GET("", a.RecordAPI.Query)
		//product.DELETE(":id", a.GoodsAPI.Delete)
	}
	// 联系 我们 Connection
	connection := v1.Group("admin_connection")
	{
		//product.GET(":id", a.RecordAPI.Get)
		connection.GET("", a.ConnectionAPI.Query)
		//product.DELETE(":id", a.GoodsAPI.Delete)
	}
	return nil
}
